/*
 * @Author: 颜礼田 yanlitian@carleagroup.com
 * @Date: 2024-08-14 13:25:55
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-27 11:06:19
 * @FilePath: \server\src\validate\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ApiProperty } from '@midwayjs/swagger';
import { ListParams, Login } from '../interface';
import { Rule, RuleType } from '@midwayjs/validate';

export const alternatives = [
  RuleType.string().allow(''),
  RuleType.object().allow(null),
  RuleType.string().allow(),
  RuleType.number().allow(0),
  RuleType.array().allow(null),
  RuleType.boolean().allow(),
  RuleType.date().allow(),
  RuleType.date().allow(null),
];

export class ListParamsValidator<T> implements ListParams<T> {
  @ApiProperty({
    type: 'number',
    title: '页数',
    example: 1,
    description: '页数',
    required: true,
  })
  @Rule(RuleType.number().required())
  pageNum: number;
  @ApiProperty({
    type: 'number',
    title: '每页条数',
    example: 10,
    description: '每页条数',
    required: true,
  })
  @Rule(RuleType.number().required())
  pageSize: number;
  @ApiProperty({
    type: 'string',
    title: '排序字段',
    example: 'createAt',
    description: '排序字段',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  sortField?: string;
  @ApiProperty({
    type: 'string',
    title: '排序方式',
    example: 'DESC',
    description: '排序方式',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  sortMethod?: string;
  @ApiProperty({
    type: 'object',
    title: '搜索参数',
    example: {},
    description: '搜索参数',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  search?: T;
}

export class StrIdValidator {
  @ApiProperty({
    type: 'string',
    title: 'ID',
    example: '419d3e4b-6445-4bc4-a74b-f0c2afb470ca',
    description: 'id',
    required: true,
  })
  @Rule(RuleType.string().required())
  id: string;
}

export class StrIdsValidator {
  @ApiProperty({
    type: 'array',
    items: {
      type: StrIdValidator,
    },
    title: 'IDS',
    example: {
      ids: ['419d3e4b-6445-4bc4-a74b-f0c2afb470ca'],
    },
    description: 'id',
    required: true,
  })
  @Rule(RuleType.array().required())
  ids: string[];
}

export class StrIdNullValidator {
  @ApiProperty({
    type: 'string',
    title: 'ID',
    example: '419d3e4b-6445-4bc4-a74b-f0c2afb470ca',
    description: 'id',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  id?: string;
}

export class LoginValidator implements Login {
  @ApiProperty({
    type: 'string',
    title: 'ID',
    example: 'admin',
    description: 'id',
    required: true,
  })
  @Rule(RuleType.string().required())
  username: string;

  @ApiProperty({
    type: 'string',
    title: 'ID',
    example: '123456',
    description: 'id',
    required: true,
  })
  @Rule(RuleType.string().required())
  password: string;
}

export class CounterValidator {
  @ApiProperty({
    type: 'number',
    title: '数量',
    example: '5',
    description: '获取条数',
    required: true,
  })
  @Rule(RuleType.number().required())
  counter: number;
}

export class ChangePwdValidator {
  @ApiProperty({
    type: 'string',
    title: '验证码',
    example: '509848',
    description: '验证码',
    required: true,
  })
  @Rule(RuleType.string().required())
  code: string;

  @ApiProperty({
    type: 'string',
    title: 'codeKey',
    example: '',
    description: 'codeKey',
    required: true,
  })
  @Rule(RuleType.string().required())
  codeKey: string;

  @ApiProperty({
    type: 'string',
    title: '新密码',
    example: '',
    description: '新密码',
    required: true,
  })
  @Rule(RuleType.string().required())
  password: string;
}
